package com.sxc.study;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AlarmManagerActivity extends AppCompatActivity {

    private AlarmManager alarmManager;
    private TextView tv;
    private PendingIntent pendingIntent;

    private AlarmBroadcastReceiver receiver = new AlarmBroadcastReceiver(new AlarmBroadcastReceiver.OnAlarmListener() {
        @Override
        public void onAlarmed() {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.CHINA);
            String time = format.format(new Date(System.currentTimeMillis()));
            tv.setText((tv.getText() + "\n" + time + "：alarm"));
            set(60 * 1000);
        }
    });
    private DHPttReceiver dhPttReceiver = new DHPttReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_manager);
        alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
        tv = findViewById(R.id.tv);


        findViewById(R.id.alarm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("DHPttReceiver", "onClick: test" );
//                set(0);
            }
        });

//        AlarmBroadcastReceiver.registerReceiver(APP.getAPP(), receiver);
        DHPttReceiver.registerReceiver(APP.getAPP(), dhPttReceiver);
    }

    private void set(long delay) {
        Intent intent = new Intent();
        intent.setAction(AlarmBroadcastReceiver.ACTION_ALARM);
        pendingIntent = PendingIntent.getBroadcast(AlarmManagerActivity.this, 0, intent, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);
        } else {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);
        }
    }

    private void setRepeating(long delay) {
        Intent intent = new Intent();
        intent.setAction(AlarmBroadcastReceiver.ACTION_ALARM);
        pendingIntent = PendingIntent.getBroadcast(AlarmManagerActivity.this, 0, intent, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), delay, pendingIntent);
        } else {
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), delay, pendingIntent);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        alarmManager.cancel(pendingIntent);
//        AlarmBroadcastReceiver.unregisterReceiver(APP.getAPP(), receiver);
        DHPttReceiver.unregisterReceiver(APP.getAPP(), dhPttReceiver);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
}
